package cn.lg.soar.general.service.impl;

import cn.lg.soar.common.data.tree.ITreeResult;
import cn.lg.soar.common.data.tree.TreeOption;
import cn.lg.soar.common.util.current.CurrentProxy;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.general.entity.Region;
import cn.lg.soar.general.mapper.RegionMapper;
import cn.lg.soar.general.service.IRegionService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Region服务层实现
 * 区域
 * @author luguoxiang
 * @since 2022-08-04
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class RegionServiceImpl extends BaseServiceImpl<RegionMapper, Region> implements IRegionService, CurrentProxy<IRegionService> {

    @Caching(evict = {
            @CacheEvict(value = "region:id", key = "#entity.id"),
            @CacheEvict(value = "region:ls", key = "#entity.id"),
            @CacheEvict(value = "region", key = "'tree'")
    })
    @Override
    public boolean updateById(Region entity) {
        return super.updateById(entity);
    }

    @Cacheable(cacheNames = "region:id", key = "#id")
    @Override
    public Region getById(Serializable id) {
        return super.getById(id);
    }

    @Cacheable(value = "region", key = "'tree'")
    @Override
    public List<TreeOption<Integer>> tree() {
        List<Region> list = list(
                Wrappers.<Region>lambdaQuery()
                        .select(Region::getId, Region::getName, Region::getStatus)
                        .orderByAsc(Region::getSort)
        );
        return ITreeResult.valueOf(list, 0, x -> {
            TreeOption<Integer> result = new TreeOption<>();
            result.setValue(x.getId());
            result.setLabel(x.getName());
            if (x.getStatus() == 1) {
                result.setDisabled(true);
            }
            return result;
        });
    }

    @Override
    public List<Region> getRegionPath(int id) {
        // 获取省
        IRegionService proxy = proxy();
        int provinceId = id & 0b110000;
        Region province = proxy.getById(provinceId);
        if (province == null) {
            return Collections.emptyList();
        }
        List<Region> list = new ArrayList<>(3);
        list.add(province);
        if (id == provinceId) {
            return list;
        }
        // 获取市
        int cityId = id & 0b111100;
        Region city = proxy.getById(cityId);
        if (city == null) {
            return list;
        }
        list.add(city);
        if (id == cityId) {
            return list;
        }
        // 获取区（县）
        Region district = proxy.getById(id);
        if (district == null) {
            return list;
        }
        list.add(district);
        return list;
    }

    @Cacheable(value = "region:ls", key = "#id")
    @Override
    public List<String> getNameList(int id) {
        return getRegionPath(id).stream()
                .map(Region::getName)
                .collect(Collectors.toList());
    }

    @Override
    public Map<Integer, List<String>> getNameListMap(Collection<Integer> ids) {
        Map<Integer, List<String>> map = new HashMap<>(ids.size());
        for (Integer id : ids) {
            map.put(id, getNameList(id));
        }
        return map;
    }
}
